package com.UI;

import java.util.Calendar;
import java.util.List;

import com.Model.CurricularDbAdapter;
import com.Model.StudentDBModel;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

/** Author Anirudh, Mohammed
 * 
 */

public class ToDoList extends ListActivity{

	 private ListView mylist;
	 List<RetriveToDoInfo> records;
	 RetriveToDoInfo[] todo_arr;
	 String s;
	 String[] menuItems = {"Delete"};
	 protected static final int CONTEXTMENU_DELETEITEM = 0;
	 StudentDBModel model;
	 private ToDoAdapter todo_adapter;
	private List<String> desc;
	private String[] todo_desc;
	
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.to_do_list);
    	getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.todo_titlebar);
    	
    	((TextView)findViewById(R.id.TodoTitle)).setText("TO-DO");
    	
    	
    	
    	model = new StudentDBModel(this);
    	
    	findViewById(R.id.back).setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			ToDoList.this.finish();

    		}
    	});

    	
    	
    	findViewById(R.id.plus).setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			ToDoList.this.finish();
    			Intent addToDo = new Intent();
    			addToDo.setClassName("com.UI", "com.UI.AddToDo");
    			startActivity(addToDo);
    			
    		}
    	});
    	
        
        
        mylist= getListView();

        mylist.setTextFilterEnabled(true);

        refreshToDo();
        
        registerForContextMenu(mylist);

   
       
    }
     
     
     public void onCreateContextMenu(ContextMenu menu, View v,
         ContextMenuInfo menuInfo) {
         AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
         
         //menu.setHeaderTitle(todo_arr[info.position]);

         for (int i = 0; i<menuItems.length; i++) {
           menu.add(Menu.NONE, i, i, menuItems[i]);
         }
     
       
       
     }
     
     
     @Override
     public boolean onContextItemSelected(MenuItem item) {
       AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
       int menuItemIndex = item.getItemId();
       String menuItemName = menuItems[menuItemIndex];
       String listItemName = todo_desc[info.position];

       // Switch on the ID of the item, to get what the user selected. 

       switch (menuItemIndex) {

            	   
              case CONTEXTMENU_DELETEITEM:
            	  
            	  // Remove it from the list.

            	  model.deleteToDo(listItemName);
            	  
                  refreshToDo();

                  return true; // true means: "we handled the event". 
            	   
            	   
                      

       }

       return false;
     }
     
     
     public void refreshToDo(){
    	 
    	    records = model.selectToDo();
    	    desc = model.selectToDoDesc();
    	   // todo_arr = (RetriveToDoInfo[])records.toArray(new String[records.size()]);
    	    todo_desc = (String[])desc.toArray(new String[desc.size()]);
    	 
            this.todo_adapter = new ToDoAdapter(this, R.layout.todo_row, records);
            setListAdapter(this.todo_adapter);
    	 
     }
     

	
    
}
